package entity

import (
	"time"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	// TbGuild 表名
	TbGuild db.TbName = "guilds"
)

// 公会表列名
const (
	GuildName        db.TbCol = "name"
	GuildDescription db.TbCol = "description"
	GuildPresidentId db.TbCol = "president_id"
	GuildLevel       db.TbCol = "level"
	GuildExp         db.TbCol = "exp"
	GuildAutoPass    db.TbCol = "auto_pass"
)

// Guild 公会实体
type Guild struct {
	migrate.OneModel
	Name        string `gorm:"size:50;comment:公会名称"`
	Description string `gorm:"size:255;default:'';comment:公会描述"`
	PresidentId uint64 `gorm:"comment:会长角色ID"`
	Level       uint32 `gorm:"default:1;comment:公会等级"`
	Exp         uint64 `gorm:"default:0;comment:公会经验"`
	AutoPass    bool   `gorm:"default:false;comment:是否自动通过"`
}

// NewGuild 创建新公会
func NewGuild() *Guild {
	ret := &Guild{}

	ret.ID = snowflake.GetId()
	ret.SetLevel(1)
	ret.SetExp(common.Zero)
	ret.SetCreatedAt(time.Now())
	ret.SetUpdatedAt(time.Now())
	ret.SetAutoPass(false)
	return ret
}

// Guild setters
func (g *Guild) SetName(name string) {
	g.Name = name
	g.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuild, GuildName, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: name,
	})
}
func (g *Guild) SetAutoPass(autoPass bool) {
	g.AutoPass = autoPass
	g.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuild, GuildAutoPass, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: autoPass,
	})
}

func (g *Guild) SetDescription(description string) {
	g.Description = description
	g.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuild, GuildDescription, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: description,
	})
}

func (g *Guild) SetPresidentId(presidentId uint64) {
	g.PresidentId = presidentId
	g.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuild, GuildPresidentId, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: presidentId,
	})
}

func (g *Guild) SetLevel(level uint32) {
	g.Level = level
	g.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuild, GuildLevel, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: level,
	})
}

func (g *Guild) SetExp(exp uint64) {
	g.Exp = exp
	g.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuild, GuildExp, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: exp,
	})
}

func (g *Guild) SetCreatedAt(time time.Time) {
	g.CreatedAt = time
	syndb.AddDataToLazyChan(TbGuild, db.CreatedAtName, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: time,
	})
}

func (g *Guild) SetUpdatedAt(time time.Time) {
	g.UpdatedAt = time
	syndb.AddDataToLazyChan(TbGuild, db.UpdatedAtName, &syndb.ColData{
		IdVal:  g.ID,
		ColVal: time,
	})
}

func initGuild() {
	// 注册Guild表
	syndb.RegLazyWithSmall(TbGuild, db.CreatedAtName)
	syndb.RegLazyWithSmall(TbGuild, db.UpdatedAtName)
	syndb.RegLazyWithSmall(TbGuild, GuildName)
	syndb.RegLazyWithSmall(TbGuild, GuildDescription)
	syndb.RegLazyWithSmall(TbGuild, GuildPresidentId)
	syndb.RegLazyWithSmall(TbGuild, GuildLevel)
	syndb.RegLazyWithSmall(TbGuild, GuildExp)
	syndb.RegLazyWithSmall(TbGuild, GuildAutoPass)

	// 自动迁移表结构
	migrate.AutoMigrate(&Guild{})

}
